package be.webchess.www.actions.ajax;

import java.util.TreeMap;

import be.webchess.www.logic.impl.ChessGameLogic;
import be.webchess.www.model.ChessBoard;
import be.webchess.www.service.IChessBoardService;
import be.webchess.www.service.impl.ChessBoardService;

public class AjaxMoveRequestAction extends AjaxBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9205309109730707119L;

	private IChessBoardService		cbServ;
	private ChessBoard				cb;
	private ChessGameLogic			logic;
	
	public String execute ()
	{
		try
		{
			if ( request.getParameter("chessboard_id") == null || request.getParameter("from") == null )
			{
				this.addActionError("Invalid action");
				return ERROR;
			}
			
			Long cb_id = Long.parseLong(request.getParameter("chessboard_id"));
			
			cbServ = new ChessBoardService(connection);
			
			cb = cbServ.findByID(cb_id);
			
			logic = new ChessGameLogic(cb);
					

			
		} catch ( Exception ex )
		{			
		}
		
		return SUCCESS;	
	}
	
	@Override
	public String getContent() {
		// TODO Auto-generated method stub
		try {
			String str = "";
			
			for ( String s : logic.getCorrectMovePositions(request.getParameter("from")) )
			{
				str += s + ", ";
			}
			
			if ( str.endsWith(", ") )
				str = str.substring(0, str.length()-2);
			
			return str;
		} catch ( Exception ex )
		{
			return "ERROR";
		}
	}

}
